﻿using System;
using System.IO;
using System.Xml;
using System.Text;
using EMANIFEST.DataAccess.Models;
using EMANIFEST.Dtos;
using System.Linq;
using System.Collections.Generic;

namespace EMANIFEST.DataAccess
{
    public partial class BusinessDataAccess
    {
        private enum enumFormImportXML
        {
            GoodDeclaration = 1,    //has child GoodDeclarationItem
            HouseBill = 2,          //has child HouseBillItem
            GeneralDeclaration = 3,
            Crew = 4,
            CrewEffectDeclaration = 5,
            ShipStoreDeclaration = 6,   //has child ShipStoreItem
            Passenger = 7,
            DangerousGood = 8       //has child DangerousGoodItem
        }

        public void ImportGoodDeclarationXMLDetail(GoodDeclarationDto objGoodDeclarationDto, enumUploadType uploadType)
        {
            try
            {
                //goodDeclarationDto.IsTemp = IsSubmitDocument(goodDeclarationDto.DocumentId);

                ////Update document status
                //if (goodDeclarationDto.IsTemp)
                //{
                //    SetDocumentStatusUpdate(goodDeclarationDto.DocumentId, enumDocumentUpdate.GoodDeclaration);
                //}

                Dto2Xml objDto2Xml = new Dto2Xml();

                string xmlString = objDto2Xml.GoodDeclaration2XML(objGoodDeclarationDto);
                Console.WriteLine(xmlString);
                //CreateXMLStringGoodDeclaration(goodDeclarationDto.GoodItems, goodDeclarationDto);
                var retVal = new System.Data.Objects.ObjectParameter("retVal", typeof(int));

                #region CheckTestLocation
                ////test location
                //Guid docId = objGoodDeclarationDto.DocumentId;
                //Guid? creId = objGoodDeclarationDto.CreaterId;
                //int formImport = (int)enumFormImportXML.GoodDeclaration;
                #endregion

                if (uploadType == enumUploadType.Addition || uploadType == enumUploadType.Original)
                {
                    //add good items   
                    //  - add new parent and child or
                    //  - add only child
                    eManifestModel.SaveDocumentItemXml(objGoodDeclarationDto.DocumentId,
                                                        objGoodDeclarationDto.CreaterId,
                                                        xmlString,
                                                        false,
                                                        (int)enumFormImportXML.GoodDeclaration,
                                                        retVal);
                }
                else if (uploadType == enumUploadType.Replace)
                {
                    //Update all GoodItems and GoodDeclaration
                    //1. delete old good items
                    //2. update parent GoodDeclaration
                    //3. insert new good items
                    eManifestModel.SaveDocumentItemXml(objGoodDeclarationDto.DocumentId,
                                                        objGoodDeclarationDto.CreaterId,
                                                        xmlString,
                                                        true,
                                                        (int)enumFormImportXML.GoodDeclaration,
                                                        retVal);
                }
                else    //uploadType == enumUploadType.Unknow || uploadType == enumUploadType.Cancellation 
                    return;

            }
            catch (Exception bl)
            {
                Console.WriteLine(bl.InnerException.Message);
            }
        }

        public void ImportGeneralDeclarationXMLDetail(List<GeneralDeclarationDto> listGeneralDeclarationDto, enumUploadType uploadType)
        {
            try
            {
                Dto2Xml objDto2Xml = new Dto2Xml();

                string xmlString = objDto2Xml.GeneralDeclaration2XML(listGeneralDeclarationDto);
                Console.WriteLine(xmlString);
                var retVal = new System.Data.Objects.ObjectParameter("retVal", typeof(int));

                #region CheckTestLocation
                ////test location
                //Guid docId = objGoodDeclarationDto.DocumentId;
                //Guid? creId = objGoodDeclarationDto.CreaterId;
                //int formImport = (int)enumFormImportXML.GoodDeclaration;
                #endregion

                if (uploadType == enumUploadType.Addition || uploadType == enumUploadType.Original)
                {
                    //addition general items   
                    eManifestModel.SaveDocumentItemXml(listGeneralDeclarationDto[0].DocumentId,
                                                        listGeneralDeclarationDto[0].CreaterId,
                                                        xmlString,
                                                        false,
                                                        (int)enumFormImportXML.GeneralDeclaration,
                                                        retVal);
                }
                else if (uploadType == enumUploadType.Replace)
                {
                    //Update all GeneralDeclaration
                    //1. delete old general items
                    //2. insert new general items
                    eManifestModel.SaveDocumentItemXml(listGeneralDeclarationDto[0].DocumentId,
                                                        listGeneralDeclarationDto[0].CreaterId,
                                                        xmlString,
                                                        true,
                                                        (int)enumFormImportXML.GeneralDeclaration,
                                                        retVal);
                }
                else    //uploadType == enumUploadType.Unknow || uploadType == enumUploadType.Cancellation 
                    return;
            }
            catch(Exception bl)
            {
                bl.ToString();
            }
        }

        public void DangerousGoodXML_Sub(DangerousGoodDto dg, enumUploadType uploadType)
        {

            try
            {

                Dto2Xml objDto2Xml = new Dto2Xml();

                string xmlString = objDto2Xml.DangerousGood2XML(dg);
                Console.WriteLine(xmlString);

                var retVal = new System.Data.Objects.ObjectParameter("retVal", typeof(int));

                if (uploadType == enumUploadType.Addition || uploadType == enumUploadType.Original)
                {
                    //addition good items                   
                    eManifestModel.SaveDocumentItemXml(dg.DocumentId, dg.CreaterId, xmlString, false, (int)enumFormImportXML.DangerousGood, retVal);
                }
                else if (uploadType == enumUploadType.Replace)
                {
                    //Update all GoodItems and GoodDeclaration
                    //1. delete old good items
                    //2. update parent GoodDeclaration
                    //3. insert new good items
                    eManifestModel.SaveDocumentItemXml(dg.DocumentId, dg.CreaterId, xmlString, true, (int)enumFormImportXML.DangerousGood, retVal);
                }
                else    //uploadType == enumUploadType.Unknow || uploadType == enumUploadType.Cancellation 
                    return;

            }
            catch (Exception bl)
            {
                bl.ToString();
            }

        }    
    }

}
